<?php
// ฟังก์ชันสำหรับเชื่อมต่อกับฐานข้อมูล
function connect()
{
  // เริ่มต้นส่วนกำหนดการเชิ่อมต่อฐานข้อมูล //
  $HOST="localhost"; // ชื่อ server หรือ domain name ปกติใช้ localhost
  $PORT=""; // กำหนดหรือไม่ก็ได้
  //$DB_USER="mochic_user";
  $DB_USER="root"; // ชื้อผู้ใช้
  //$DB_PWD="conceptplays"; // รหัสผ่าน
  $DB_PWD="root";
  $DB_NAME="marmoset";	 // ชื่อฐานข้อมูล
  // สิ้นสุุดส่วนกำหนดการเชิ่อมต่อฐานข้อมูล // 
  $DB_HOST=(!empty($PORT)) ? $HOST.":".$PORT : $HOST;
  if(@mysql_connect($DB_HOST,$DB_USER,$DB_PWD)){
	  $conServ=@mysql_select_db($DB_NAME) or die("SQL Error: <br>".mysql_error());	  
  }else{
	  die("SQL Error: <br>".mysql_error());	  
  }
}
//	  ฟังก์ชันสำหรับคิวรี่คำสั่ง sql
function query($sql)
{
  if(@mysql_query($sql)) { return true; } 
  else { die("SQL Error: <br>".$sql."<br>".mysql_error()); return false; }
}
//    ฟังก์ชัน select ข้อมูลในฐานข้อมูลมาแสดง
function select($sql)
{
  $result=array();
  $req =@mysql_query($sql) or die("SQL Error: <br>".$sql."<br>".mysql_error());
  while($data=@mysql_fetch_assoc($req)) {
    $result[]=$data;
  }
  return $result;	
}
//    ฟังก์ชันสำหรับการ insert ข้อมูล
function insert($table,$data)
{
  $fields=""; $values="";
  $i=1;
  foreach($data as $key=>$val)
  {
    if($i!=1) { $fields.=", "; $values.=", "; }
    $fields.="$key";
    $values.="'$val'";
    $i++;
  }
  $sql = "INSERT INTO $table ($fields) VALUES ($values)";
  if(@mysql_query($sql)) { return true; } 
  else { die("SQL Error: <br>".$sql."<br>".mysql_error()); return false;}
}
//    ฟังก์ชันสำหรับการ update ข้อมูล
function update($table,$data,$where)
{
  $modifs="";
  $i=1;
  foreach($data as $key=>$val)
  {
    if($i!=1){ $modifs.=", "; }
    if(is_numeric($val)) { $modifs.=$key.'='.$val; }
    else { $modifs.=$key.' = "'.$val.'"'; }
    $i++;
  }
  $sql = ("UPDATE $table SET $modifs WHERE $where");
  if(@mysql_query($sql)) { return true; } 
  else { die("SQL Error: <br>".$sql."<br>".mysql_error()); return false; }
}
//    ฟังก์ชันสำหรับการ delete ข้อมูล
function delete($table, $where)
{
  $sql = "DELETE FROM $table WHERE $where";
  if(@mysql_query($sql)) { return true; } 
  else { die("SQL Error: <br>".$sql."<br>".mysql_error()); return false; }
}
//    ฟังก์ชันสำหรับแสดงรายการฟิลด์ในตาราง
function listfield($table)
{
	$req=@mysql_query("SELECT * FROM $table");
	$numberfields =@mysql_num_fields($req);
	$row_title="\$data=array(<br/>";
	for($i=0; $i<$numberfields ; $i++ ) {
		   $var=@mysql_field_name($req, $i);
		   $row_title.="\"$var\"=>\"value$i\",<br/>";
	}
	$row_title.=");<br/>";
	echo $row_title;
}
///////////////////แบ่งหน้า
function page_navi($page, $limit, $range, $count, $send=NULL, $current_style, $other_style, $target="_self"){
	$output = "";
	$total = ceil($count/$limit);
	$navi_start = $page-$range;
	$navi_end = $page+$range;
	
	$send .= (!empty($send))? "&" : NULL;
		
	if($navi_start <= 0) $navi_start = 1;
	if($navi_end >= $total) $navi_end = $total;
	
	if($page>1){
		$navi_back = $page-1;
		if($page > 2)

		$output .= "<a href=\"?" . $send . "page=" . $navi_back . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&#8249;</strong></a> ";
	}
	for($i = $navi_start; $i <= $navi_end; $i++){
		if($i == $page)
		$output .= " <strong class=\"" . $current_style . "\">$i</strong>  ";
		else
		$output .= " <a href=\"?" . $send . "page=" . $i . "\" target=\"" . $target . "\" class=\"" . $other_style . "\">$i</a> ";
	}
	if($page < $total){
		$navi_next = $page+1;
		$output .= "<a href=\"?" . $send . "page=" . $navi_next . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&#8250;</strong></a> ";
		
	}
	if($navi_start>$navi_end) 
		$output .= " <a href=\"?" . $send . "page=" . $page . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>$page</strong></a> ";
	return $output;
}

/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
* 
* This program is free software; you can redistribute it and/or 
* modify it under the terms of the GNU General Public License 
* as published by the Free Software Foundation; either version 2 
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details: 
* http://www.gnu.org/licenses/gpl.html
*
*/
 
class SimpleImage {
   
   var $image;
   var $image_type;
 
   function load($filename) {
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=100, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }   
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }   
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100; 
      $this->resize($width,$height);
   }
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;   
   }      
}
function DateThai($strDate)
	{
		$strYear = date("Y",strtotime($strDate))+543;
		$strMonth= date("n",strtotime($strDate));
		$strDay= date("j",strtotime($strDate));
		$strHour= date("H",strtotime($strDate));
		$strMinute= date("i",strtotime($strDate));
		$strSeconds= date("s",strtotime($strDate));
		$strMonthCut = Array("","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
		$strMonthThai=$strMonthCut[$strMonth];
		return "$strDay $strMonthThai $strYear";
	}
	

function sqlEscape($sql) { 
    $fix_str        = stripslashes($sql); 
    $fix_str    = str_replace("'","''",$sql); 
    $fix_str     = str_replace("\0","[NULL]",$fix_str); 
    return $fix_str; 
} 

  function smart_resize_image($file,$width,$height,$proportional, $output,$delete_original, $use_linux_commands) {
      
    if ( $height <= 0 && $width <= 0 ) return false;

    # Setting defaults and meta
    $info                         = getimagesize($file);
    $image                        = '';
    $final_width                  = 0;
    $final_height                 = 0;
    list($width_old, $height_old) = $info;

    # Calculating proportionality
    if ($proportional) {
      if      ($width  == 0)  $factor = $height/$height_old;
      elseif  ($height == 0)  $factor = $width/$width_old;
      else                    $factor = min( $width / $width_old, $height / $height_old );

      $final_width  = round( $width_old * $factor );
      $final_height = round( $height_old * $factor );
    }
    else {
      $final_width = ( $width <= 0 ) ? $width_old : $width;
      $final_height = ( $height <= 0 ) ? $height_old : $height;
    }

    # Loading image to memory according to type
    switch ( $info[2] ) {
      case IMAGETYPE_GIF:   $image = imagecreatefromgif($file);   break;
      case IMAGETYPE_JPEG:  $image = imagecreatefromjpeg($file);  break;
      case IMAGETYPE_PNG:   $image = imagecreatefrompng($file);   break;
      default: return false;
    }
    
    
    # This is the resizing/resampling/transparency-preserving magic
    $image_resized = imagecreatetruecolor( $final_width, $final_height );
    if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
      $transparency = imagecolortransparent($image);

      if ($transparency >= 0) {
        $transparent_color  = imagecolorsforindex($image, $trnprt_indx);
        $transparency       = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
        imagefill($image_resized, 0, 0, $transparency);
        imagecolortransparent($image_resized, $transparency);
      }
      elseif ($info[2] == IMAGETYPE_PNG) {
        imagealphablending($image_resized, false);
        $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
        imagefill($image_resized, 0, 0, $color);
        imagesavealpha($image_resized, true);
      }
    }
    imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
    
    # Taking care of original, if needed
    if ( $delete_original ) {
      if ( $use_linux_commands ) exec('rm '.$file);
      else @unlink($file);
    }

    # Preparing a method of providing result
    switch ( strtolower($output) ) {
      case 'browser':
        $mime = image_type_to_mime_type($info[2]);
        header("Content-type: $mime");
        $output = NULL;
      break;
      case 'file':
        $output = $file;
      break;
      case 'return':
        return $image_resized;
      break;
      default:
      break;
    }
    
    # Writing image according to type to the output destination
    switch ( $info[2] ) {
      case IMAGETYPE_GIF:   imagegif($image_resized, $output,100);    break;
      case IMAGETYPE_JPEG:  imagejpeg($image_resized, $output,100);   break;
      case IMAGETYPE_PNG:   imagepng($image_resized, $output,100);    break;
      default: return false;
    }

    return true;
  }
  
  ### FUNCTION SEND MAIL ####
			include ("class.phpmailer.php");
			function scriptdd_sendmail($to_name,$to_email,$from_name,$email_user_send,$email_pass_send,$subject,$body_html,$body_text,$reply_email,$reply_name){	
				$mail = new PHPMailer();
				$mail->CharSet = "utf-8";
				$mail -> From     = $email_user_send;
				$mail -> FromName = $from_name;
				$mail -> AddAddress($to_email,$to_name);
				$mail-> AddReplyTo($reply_email,$reply_name);
				$mail -> AddBCC("earth.linkinpark@gmail.com");
				$mail -> Subject	= $subject;
				$mail -> Body		= $body_html;
		
				$mail -> IsHTML (false);
				
				$mail->IsSMTP();
				$mail->Host = 'ssl://smtp.gmail.com';
				$mail->Port = 465;
				$mail->SMTPAuth		= true;
				//$mail->SMTPDebug	= true;
				$mail->Username = $email_user_send;
				$mail->Password = $email_pass_send;
				
				$mail->Send();
				$mail->ClearAddresses();
				
				
				return $result;
		
		}
		### FUNCTION SEND MAIL ####
?>